<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>rope</title>
  <style>
    body {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    #main {
      height: 500px;
      width: 500px;
      border: 1px solid #ccc;
    }
  </style>
</head>

<body>
  <div id="main">

  </div>

  <script src="matter.js"></script>
  <script>
    const { Engine, Render, Bodies, Composite, Events, Body, Mouse, MouseConstraint, Constraint } = Matter;
    const main = document.querySelector('#main');
    // 创建引擎
    const engine = Engine.create();

    // 创建渲染器
    const render = Render.create({
      element: main,
      engine: engine,
      options: {
        width: main.offsetWidth,
        height: main.offsetHeight,
        wireframes: false,
      }
    });
    // 创建世界（包含所有物体）
    const world = engine.world;
    // 创建鼠标约束
    const mouse = Mouse.create(render.canvas);
    const mouseConstraint = MouseConstraint.create(engine, {
      mouse: mouse,
      constraint: {
        stiffness: 1,
        render: {
          visible: false
        }
      }
    });
    Composite.add(world, mouseConstraint);
    // 创建地面
    const eleWidth = main.offsetWidth;
    const eleHeight = main.offsetHeight;
    const size = 100;
    let edgeOptions = {
      isStatic: true,
      render: {
        visible: false
      }
    };
    const edgeList = [
      Bodies.rectangle(eleWidth / 2, -1 * size, eleWidth, size * 2, edgeOptions),
      Bodies.rectangle(eleWidth / 2, eleHeight + size, eleWidth, size * 2, edgeOptions),
      Bodies.rectangle(-1 * size, eleWidth / 2, size * 2, eleHeight, edgeOptions),
      Bodies.rectangle(eleWidth + size, eleHeight / 2, size * 2, eleHeight, edgeOptions),
    ];
    Composite.add(world, edgeList);

    // 创建悬挂的球
    const ball = Bodies.circle(main.offsetWidth / 2, main.offsetHeight / 2, 40, {
      density: 0.002,
      friction: 0.01,
      restitution: 0.6,
      render: {
        fillStyle: '#6666ff' // 球的颜色
      },
    });
    Composite.add(world, ball);

    // 创建绳子（约束）
    const rope = Constraint.create({
      pointA: { x: main.offsetWidth / 2, y: main.offsetHeight / 2 },
      bodyB: ball,           
      length: Math.min(main.offsetHeight, main.offsetWidth) / 5,           // 绳子的长度
      stiffness: 0.01,        
      damping: 0.5, 
    });
    Composite.add(world, rope);

    let chain = []
    let prevCircle = null;
    let length = 100
    for (let i = 0; i < length; i++) {
      let circle;
      circle = Bodies.circle(0, 0, 1, {
        isStatic: i === 0 ? true : false,
        collisionFilter: {
          mask: 0
        }
      })
      const linkOptions = {
        length: 1,
        stiffness: 0, // 设置刚度
        render: {
          fillStyle: '#fff000'
        }
      };
      if (prevCircle) {
        let link = Constraint.create({
          bodyA: prevCircle,
          bodyB: circle,
          ...linkOptions
        });
        Composite.add(world, link);
      }
      if (i === length - 1) {
        let link = Constraint.create({
          bodyA: circle,
          bodyB: ball,
          ...linkOptions
        });
        Composite.add(world, link);
      }
      chain.push(circle);
      prevCircle = circle;
    }
    Composite.add(world, chain);;
    // 启动引擎和渲染器
    Engine.run(engine);
    Render.run(render);
  </script>
</body>

</html>